<script type="text/javascript">
	var aSelectedRows = [];
	
	$(function(){
		$('#period').monthpicker({
			pattern: 'yyyy-mm',
		});
	});
	function exportToExcel(){
		alert("export data !");
	}
	function reloadData(){
		$("#tblData").jqGrid("GridUnload");
		$("#tblData").jqGrid({
			url:'index.php?r=keuangan/tu/report/BCA_VADetailList',
			//editUrl:'index.php?r=akademik/pendaftaran_kelas/kelas_tahun_ajaran/edit',
			postData:{"period":$("#period").val()},
			datatype: 'json',
			loadonce:true,
			mtype:"GET",
			autowidth:false,
			altRows:true,
			ignoreCase:true,
			
			colNames: ['id','Student ID','Name','Class','Total Amount','BCA VA'],
			colModel: [
				{name: 'id', index: 'id', width: 20,hidden:false,sorttype:'int',formatter:'int'},
				{name: 'sid', index: 'sid',width: 100,search:true},
				{name: 'student_name', index: 'student_name', width: 400,search:true},
				{name: 'class_name', index: 'class_name', width: 150,search:true},
				{name: 'ttl_amount', index: 'ttl_amount', width: 100,template:moneyTemplate},
				{name: 'BCA_VA', index: 'BCA_VA', width: 100,search:true},
			],
			height:"auto",
			gridview: true,
			rownumbers: false,
			rowNum: 25,
			rowList: [25, 50,100,999999],
			pager: '#navTblData',
			viewrecords: true,
			caption: 'BCA Virtual Account',
			jsonReader : { 
				root:"model",
				repeatitems: false			
			},
			loadComplete:function(data){
				console.log(data);
			},
			multiselect:true,
			onSelectRow: function (rowid,status, e) {
				if(typeof e === "undefined") {
					return;
				}
				var index = aSelectedRows.indexOf(rowid);
				if (status){
					if (index == -1){
						aSelectedRows.push(rowid);
					}
				}else{
					if (index != -1){
						aSelectedRows.splice(index, 1);
					}
				}
			},
			
			onSelectAll: function (aRowids,status) {
				arrData = $("#tblData").jqGrid("getGridParam","data");
				if (status){
					aSelectedRows = [];
					for (i=0;i<arrData.length;i++){
						aSelectedRows.push(arrData[i].id);
					}
				}else{
					aSelectedRows = [];
				}
				printConsole(aSelectedRows); 
			},
			gridComplete: function(){ 
				var selectedIds = $("#tblData").jqGrid('getGridParam','selarrrow');
				for (i=0;i<aSelectedRows.length;i++){
					rowid = aSelectedRows[i];
					var index = selectedIds.indexOf(rowid);
					if (index === -1){
						//$("#jqg_tblData_" + aSelectedRows[i]).prop('checked', true);
						$("#tblData").setSelection(rowid, false);
					}
				}	
			}
		});

		 
		$("#tblData").jqGrid('navGrid','#navTblData',{add:false,edit:false,search:false,del:false,refresh:false});
		var parameters = {caption:"Export to Excel", buttonicon:"ui-icon-newwin", 
				onClickButton:function(){
					//alert(aSelectedRows);
					var selectedData = [];
					for (i=0;i<aSelectedRows.length;i++){						
						//data = $("#tblData").jqGrid("getRowData",aSelectedRows[i]);
						data = $("#tblData").jqGrid("getLocalRow",aSelectedRows[i]);
						selectedData.push(data);		
					}					
					$("#arrSelectedData").val(JSON.stringify(selectedData));	
					//$("#frmData").submit();
				}, 
				position: "last", title:"", cursor: "pointer"};
		$("#tblData").jqGrid('navButtonAdd',"#navTblData",parameters);
		
		//add search text
		$("#tblData").jqGrid('addSearchText',["sid","student_name","class_name","BCA_VA"]);	
	}
	
</script>
<div>
	<form id="frmData" action="index.php?r=keuangan/tu/report/BCA_VA2Excel" method="POST">
		<label class="form" style="text-align:right;width:70px" >Period :</label>
		<input type="text" name="period" id="period" value="" style="width:100px" />
		<input type="button" value="Show Data" style="margin-left:5px" onclick="reloadData()"/>
		</br>
		<textarea name="arrSelectedData" id="arrSelectedData" rows="5" cols="30" style="display:none"></textarea>
	</form>
	<table id="tblData"></table>
	<div id="navTblData"></div>
	<div style="height:20px"></div>
	
</div>